#include "fig_enemy_dragon.h"
#include "fig_bullet.h"
#include "fig_enemybullet_master.h"
#include "fig_sfx_manager.h"

#include "math/facing.h"
#include "num/rand.h"


FIGEnemyDragon::FIGEnemyDragon(FIGEnemyCraft::ENEMY_TYPES EnemyType) : 
								FIGEnemyPathCraft(EnemyType)								
{
	m_SpriteHead.Create("miniboss_dragon_head");
	m_SpriteBodyPiece.Create("miniboss_dragon_body");
	m_SpriteTail.Create("miniboss_dragon_tail");

	m_HeadSize = m_SpriteHead.m_Size;
	m_BodySize = m_SpriteBodyPiece.m_Size;
	m_TailSize = m_SpriteTail.m_Size;

	m_Speed = 0.1f;
	m_IsHitTestCircle = false;

	m_MaxLife = m_Life = 80.0f;

	// make smaller for tail
	int j = 0;
	for(int i = PIECE_MAX_COUNT - 9 ; i <  PIECE_MAX_COUNT - 1 ; i++,j++)
	{
		m_Pieces[i].m_ScaleX = 1.0f - (0.05f * j);
	}

	
	m_ExplodeSize = 2.0f;
	m_ShootCounter.SetLimit(3.0f);
	m_ExplodSoundSfx = FIGSounds::EXPLOSION_VERYBIG;
}




void		FIGEnemyDragon::Tick(float dt)
{
    m_SpriteHead.Tick(dt);
	m_SpriteBodyPiece.Tick(dt);
	m_SpriteTail.Tick(dt);
    
    FIGEnemyPathCraft::Tick(dt);

	if(m_DelaySpawn >  0)
		return;

	if(!m_IsAlive)
		return;

	if(m_Life <= 0 && !m_bIsDying )
		return;

	if(m_Rotation > 360.0f)
		m_Rotation -= 360.0f;
	if(m_Rotation < 0.0f)
		m_Rotation += 360.0f;


	for(int i = 0; i < PIECE_MAX_COUNT; i++)
	{
		m_Pieces[i].Tick(	dt,
							m_Speed,
							m_PathType);
	}

}
	
void		FIGEnemyDragon::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
	{
		if(m_DelayDie <= 0)
			return;
	}

	if(m_DelaySpawn > 0)
		return;

	// TAIL
	m_Pieces[PIECE_MAX_COUNT -1].Render( 
							Offset,
							Scale,
							m_SpriteTail,
							m_TailSize
							);

	for(int i = PIECE_MAX_COUNT  -2 ; i >= 0 ; i--)
	{		   
		m_Pieces[i].Render( Offset,
							Scale,
							m_SpriteBodyPiece,
							m_BodySize
							);		
	}

	
	m_SpriteHead.m_Pos = m_Pos + Offset;
	m_SpriteHead.m_Rotation = m_Rotation;
	m_SpriteHead.m_Size =   m_HeadSize * Scale;
	m_SpriteHead.RenderRotated();

	

	
}
	
void		FIGEnemyDragon::ReSpawn()
{
	FIGEnemyPathCraft::ReSpawn();
	//m_Pos = math::Vec2(0.5f,0.5f);
	for(int i = PIECE_MAX_COUNT  -1 ; i >= 0 ; i--)
	{
		m_Pieces[i].m_Delay =  i * 0.3f;
		m_Pieces[i].m_Pos = m_Pos;
		m_Pieces[i].m_Rotation = m_Rotation;

		m_Pieces[i].m_PathCraftInfo.Reset();
	}

	
	m_IsReadyToFire = true;
	m_bIsDying = false;
}


void		FIGEnemyDragon::OnShoot()
{
	// shoot only downward
	if( m_Rotation <= 45 || m_Rotation >= 360 - 45)
	{}
	else
		return;


	math::Vec2 Dir = math::Vec2(0,1.0f);
	Dir.Rotate(-m_Rotation );


	FIGEnemyBulletMaster::Instance().Shoot(
											this,
											Dir * 0.15f , 
											FIGEnemyBulletMaster::BULLET_TYPES_SFX_FIRE, 
											FIGBullet::SHOOT_STYLES_SINGLE, 
											m_Rotation+180.0f,
											0,
											false,
											NULL
											);

}

bool		FIGEnemyDragon::HitTest(FIGBullet* pBullet)
{
	bool b = this->HitTest(pBullet->GetPos());
	if(b)
	{
		GotDamage(pBullet->GetDamagePower(),pBullet, false);
		pBullet->Kill();
	}
	return b;
}

bool		FIGEnemyDragon::HitTest(FIGLifeObj* pCraft)
{
	return this->HitTest(pCraft->GetPos());
}


bool		FIGEnemyDragon::HitTest(const math::Vec2& Point)
{
	if(m_Life < 0)
		return false;

	math::Vec2 BackupPos = m_Pos;
	math::Vec2 BackupSize = m_Size;

	m_pSprite = &m_SpriteHead;
	m_Size = m_HeadSize;
	bool b = FIGEnemyPathCraft::HitTest(Point);
    
	if(b)
	{
		m_pSprite = NULL;
		return true;
	}

	m_Size = m_BodySize;

	for(int i = PIECE_MAX_COUNT  -1 ; i >= 0 ; i--)
	{
		m_Pos = m_Pieces[i].m_Pos;
		m_pSprite = &m_SpriteBodyPiece;
		bool b = FIGEnemyPathCraft::HitTest(Point);
		if(b)
		{
			m_pSprite = NULL;
			m_Pos = BackupPos;
			m_Size = BackupSize;
			return true;
		}

	}

	m_pSprite = NULL;
	return false;
}



void		FIGEnemyDragon::OnStartDying()
{
	m_IsReadyToFire = false;
	FIGEnemyPathCraft::OnStartDying();
	FIGSfx* pSFX = NULL;

	for(int i = PIECE_MAX_COUNT  -1 ; i >= 0 ; i--)
	{
		
		if(i % 3== 0)
		{
			pSFX = FIGSFXManager::Instance().Spawn(
										FIGSfx::EXPLODING_VERYBIG,
										m_Pieces[i].m_Pos,
										1.0f
										);  
			if(pSFX)
			{
				AddSFX(pSFX);			
				pSFX->SetIsRenderByOwner(true);
			}
		}


	}
	m_bIsDying = true;
	m_DelayDie = 1.0f;
		
}

void			FIGEnemyDragon::OnExplode()
{
	FIGSfx* pSFX = NULL;
	FIGEnemyPathCraft::OnExplode();
	pSFX = FIGSFXManager::Instance().Spawn(	
									FIGSfx::EXPLODING_TYPE_NORMAL, 
									m_Pos,
									1.0f
									);
	if(pSFX)
	{
		AddSFX(pSFX);			
		pSFX->SetIsRenderByOwner(true);
	}

	for(int i = PIECE_MAX_COUNT  -1 ; i >= 0 ; i--)
	{
		if(i % 3== 0)
		{
			pSFX = FIGSFXManager::Instance().Spawn(	
										FIGSfx::EXPLODING_TYPE_NORMAL, 
										m_Pieces[i].m_Pos,
										1.0f
										);
			if(pSFX)
			{
				AddSFX(pSFX);			
				pSFX->SetIsRenderByOwner(true);
			}
		}
	}

	m_bIsDying = false; 
}
